﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MODEL;
using BLL;
namespace winUi
{
    public partial class FormKuCun : Form
    {
        private Dic dic = new Dic();
        private Stock stock = new Stock();
        public FormKuCun()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            GetListByPy(txtKey.Text);
        }

        private void FormKuCun_Load(object sender, EventArgs e)
        {
            Enable(false);
            dgvDic.MultiSelect = false;
            dgvDic.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvDic.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            GetListByPy("");

        }
        public void GetListByPy(string py)
        {
            dgvDic.DataSource = null;
            dgvDic.DataSource = new BllStock().GetByPY(py);
        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            GetListByPy(txtKey.Text);
        }

        private void dgvDic_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtKey.Focused && e.KeyCode == Keys.Enter)
            {
                dgvDic.Focus();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            stock.Number = Convert.ToInt32(txtNumber.Text);
            try
            {
                int b = new BllStock().UpdateStockNum(stock.Number, stock.Id);
                MessageBox.Show("修改成功！");
                GetListByPy("");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dgvDic.CurrentRow != null)
            {
                DataGridViewRow row = dgvDic.CurrentRow;
                int dicid = Convert.ToInt32(row.Cells[2].Value);
                int id = Convert.ToInt32(row.Cells[0].Value);
                dic = new BllDic().GetById(dicid);
                stock = new BllStock().GetById(id);
                txtName.Text = dic.GoodsName;
                txtModel.Text = dic.GoodsModel;
                txtSpe.Text = dic.GoodsSpe;
                txtUnit.Text = dic.GoodsUnit;
                txtPrice.Text = dic.GoodsPrice.ToString();
                txtNumber.Text = stock.Number.ToString();
                txtNumber.Focus();
            }
            else
            {
                MessageBox.Show("没有选择要编辑的行");
            }
        }
        public void Enable(bool b)
        {
            txtName.Enabled = b;
            txtModel.Enabled = b;
            txtSpe.Enabled = b;
            txtUnit.Enabled = b;
            txtPrice.Enabled = b;
            txtNumber.Focus();
        }
    }
}
